package org.example.base.common.service.impl;

import org.example.base.common.dao.TsSystemDictDao;
import org.example.base.common.entity.TsSystemDictEntity;
import org.example.base.common.model.TSystemDictItemModel;
import org.example.base.common.model.TsSystemDictModel;
import org.example.base.common.repository.TsSystemDictRepository;
import org.example.base.common.service.TSystemDictItemService;
import org.example.base.common.service.TsSystemDictService;
import org.example.core.exception.ErrorException;
import org.example.core.mysql.Where;
import org.example.core.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Description:
 * Date: 2024/1/12 22:14
 */
@Primary
@Service
public class TsSystemDictServiceImpl extends BaseServiceImpl<String, TsSystemDictModel, TsSystemDictEntity, TsSystemDictRepository, TsSystemDictDao> implements TsSystemDictService {

    @Autowired
    private TSystemDictItemService dictItemService;

    /**
     * ord升序排列
     * @param code
     * @return
     */
    @Override
    public List<TSystemDictItemModel> getByCode(String code) {
        TsSystemDictModel dictModel = searchOne(Where.build().field("code").equal(code));
        if (ObjectUtils.isEmpty(dictModel)){
            throw new ErrorException(code+"该code值不存在！");
        }
        return dictItemService.search(Where.build().field("dict_id").equal(dictModel.getId()))
                .stream().sorted(Comparator.comparingInt(TSystemDictItemModel::getOrd))
                .collect(Collectors.toList());
    }
}
